<?php
session_start();
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/database.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/functions.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/class.admin.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/class.user.php');

if(CRYPT_KEY=='') {
	exit("<script>alert('Please open ".$_SERVER['DOCUMENT_ROOT']."/config.php and enter and DEFINE CRYPT_KEY before continuing');</script>");	
	
}
if(DATABASE=='') {
	exit("<script>alert('Please open ".$_SERVER['DOCUMENT_ROOT']."/config.php and enter and DEFINE DATABSE before continuing');</script>");	
	
}

$mod=array('/inc/','/bundle/','/media/','/cache/','/themes/','/modules/','/images/');

foreach($mod as $m) {
	if(!chmod($_SERVER['DOCUMENT_ROOT'].$m,0777)) {
		exit("<script>alert('Please give the web user write access to ".$_SERVER['DOCUMENT_ROOT'].$m."');</script>");	
	}
}


$data=new database();
$data->db(DATABASE)->collection('settings');
$data->criteria(array('url'=>$_POST['site_url'],'name'=>$_POST['site_name'],'current_theme'=>'default','folder_path'=>'default'));
$_id=$data->insert();
$data->close();
/*"key" : "current_theme", "name" : "systemaddict", "folder_path" : "systemaddict" 
*/#
$data=new database();
$data->db(DATABASE)->collection('settings');
$data->criteria(array('key'=>'current_theme','name'=>'default','folder_path'=>'default','logo'=>array('name'=>'logo-default.png','width'=>intval(235),'height'=>intval(78))))->insert();
$data->close();

$data=new database();
$data->db(DATABASE);
$data->collection('settings')->criteria(array('key'=>1,'name'=>1,'folder_path'=>1,'url'=>1))->index();

$data->collection('uids')->criteria(array('uid'=>intval(rand(14000,15000))))->insert();
$data=new database();
$data->db(DATABASE);
$data->collection('uids')->criteria(array('uid'=>1))->uniqueIndex();

$data->collection('media')->criteria(array('path'=>1,'date'=>1))->index();

$data->collection('modules')->criteria(array('active'=>1,'path'=>1))->index();

$data->collection('pages')->criteria(array('active'=>1,'cache'=>1,'is_blog'=>1,'is_blog_post'=>1,'is_parent'=>1,'order'=>1,'parent'=>1,'text'=>1,'url'=>1,'content'=>1, 'include_in_nav'=>1,'post_id'=>1))->index();

$data->collection('search_index')->criteria(array('page_id'=>1,'tags'=>1,'exerpt'=>1,'url'=>1,'heading'=>1))->index();

// comments will be here for blogs to index too
// make ther bundles
$data=new database();
$data->db(DATABASE)->collection('groups');
$data->criteria(array('name'=>'Administrators'));
$row=$data->insert();
$gid=(string)$row['_id'];
$data->close();
$data=new database();
$data->db(DATABASE)->collection('groups');
$data->criteria(array('name'=>1))->uniqueIndex();
$data->close();


$admin=new admin();
$admin->createClientBundle(); // makes the Javascript and so on..

$user=new user();
$password=$user->encrypt($_POST['site_admin_password'],CRYPT_KEY);

$data->collection('users')->criteria(array(
'username'=>'Admin',
'password'=>$password,
'admin'=>intval(1),
'groups'=>array($gid),
'gravatar'=>'',
'email'=>$_POST['site_admin_email']))->insert();
$data->criteria(array('username'=>1,'groups'=>1))->index();
$data->close();

$data=new database();
$data->db(DATABASE)->collection('captcha');
$fp=file_get_contents($_SERVER['DOCUMENT_ROOT'].'/resources/lib/captcha-words.txt');
$words=trim($fp);
$words=explode("\n",$words);
$i=1;
foreach($words as $word) {
if(strlen($word)<=9) {
	$data->criteria(array('_id'=>new MongoId(),'word'=>$word,'rand'=>intval($i)))->insert();
}
$i++;
}
$data->close();


$data=new database();
$data->db(DATABASE)->collection('users');
$data->criteria(array('username'=>1))->uniqueIndex();
$data->close();

$listing=array(
'Delete page (action)'=>'/admin/resources/php/requests/actions/delete-content.php',
'Add page (action)'=>'/admin/resources/php/requests/actions/add-content.php',
'Save site settings (action)'=>'/admin/resources/php/requests/actions/save-site-settings.php',
'Create a form (action)'=>'/admin/resources/php/requests/actions/add-new-form.php',
'Delete media (action)'=>'/admin/resources/php/requests/actions/delete-image.php',
'Upload a theme (action)'=>'/admin/resources/php/requests/actions/upload_theme.php',
'Save page (action)'=>'/admin/resources/php/requests/actions/save-page.php',
'Save modules (action)'=>'/admin/resources/php/requests/actions/save-modules.php',
'Save group (action)'=>'/admin/resources/php/requests/actions/save-group.php',
'Delete page (action)'=>'/admin/resources/php/requests/actions/delete-page.php',
'Upload media (action)'=>'/admin/resources/php/requests/actions/upload_media.php',
'Delete module (action)'=>'/admin/resources/php/requests/actions/delete-module.php',
'Delete group (action)'=>'/admin/resources/php/requests/actions/delete-group.php',
'Upload logo (action)'=>'/admin/resources/php/requests/actions/upload_logo.php',
'Add new page (action)'=>'/admin/resources/php/requests/actions/add-page.php',
'Save form (action)'=>'/admin/resources/php/requests/actions/save-form.php',
'Upload Module (action)'=>'/admin/resources/php/requests/actions/upload_module.php',
'Save blog post (action)'=>'/admin/resources/php/requests/actions/save-blog-post.php',
'Add new blog post (action)'=>'/admin/resources/php/requests/actions/add-blog-post.php',
'Add group (action)'=>'/admin/resources/php/requests/actions/add-group.php',
'Show page content (list)'=>'/admin/resources/php/requests/lists/page-content.php',
'Show forms (list)'=>'/admin/resources/php/requests/lists/forms.php',
'Show pages (list)'=>'/admin/resources/php/requests/lists/pages.php',
'Show current pages (list)'=>'/admin/resources/php/requests/lists/current-pages.php',
'Show media (list)'=>'/admin/resources/php/requests/lists/media.php',
'Show blog posts / add blog post (form)'=>'/admin/resources/php/requests/forms/blog-posts.php',
'Add new group (form) / Show groups'=>'/admin/resources/php/requests/forms/add-group.php',
'Edit form (form)'=>'/admin/resources/php/requests/forms/edit-form.php',
'Edit blog post (form)'=>'/admin/resources/php/requests/forms/edit-blog-post.php',
'Edit group (form)'=>'/admin/resources/php/requests/forms/edit-group.php',
'Add new form / show form list (form)'=>'/admin/resources/php/requests/forms/forms.php',
'Show site settings (form)'=>'/admin/resources/php/requests/forms/site-settings.php',
'Add new page (form)'=>'/admin/resources/php/requests/forms/add-page.php',
'Add module (form)'=>'/admin/resources/php/requests/forms/modules.php',
'Add image (form)'=>'/admin/resources/php/requests/forms/add_image.php',
'Edit page (form)'=>'/admin/resources/php/requests/forms/edit-page.php',
'Index page'=>'/admin/index.php',
'Updater'=>'/admin/update.php'
);
$data=new database();
$data->db(DATABASE)->collection('admin_pages');
	foreach($listing as $key=>$val) {
	$data->criteria(array('$addToSet' => array("groups" => $gid)));
	$data->updateMultiple(array('path'=>$val,'role'=>'functionality','name'=>$key));

	}
$data->close();
$data=new database();
$data->db(DATABASE)->collection('pages');
$data->criteria(array('url'=>array('$exists'=>true)))->sort(array('url'=>1));
$rows=$data->findNl();
$data->close();
$data=new database();
$data->db(DATABASE)->collection('admin_pages');
	foreach($rows['results'] as $row) {
	$data->criteria(array('$addToSet' => array("groups" => $gid)));
	$data->updateMultiple(array('path'=>$row['url'],'role'=>'page','name'=>$row['text']));
	}
$data->close();
// make a bundle now ....
$admin->createAdminBundle();

$n=array(


array('label'=>'Site Settings',
	  'href'=>'/admin/resources/php/requests/forms/site-settings.php',
	  'order'=>intval(0),	
	  'is_parent'=>intval(1),
	  'parent'=>'',
	  'id'=>'list-site-settings',
	  'active'=>intval(1)
	  ),
array('label'=>'Pages / Content',
	  'href'=>'/admin/resources/php/requests/lists/pages.php',
	  'order'=>intval(1),	
	  'is_parent'=>intval(1),
	  'parent'=>'',
	  'id'=>'list-pages',
	  'active'=>intval(1)
	  ),
array('label'=>'Modules',
	  'href'=>'/admin/resources/php/requests/forms/modules.php',
	  'order'=>intval(2),	
	  'is_parent'=>intval(1),
	  'parent'=>'',
	  'id'=>'modules-click',
	  'active'=>intval(1)
	  ),
array('label'=>'Forms',
	  'href'=>'/admin/resources/php/requests/forms/forms.php',
	  'order'=>intval(3),	
	  'is_parent'=>intval(1),
	  'parent'=>'',
	  'id'=>'forms-click',
	  'active'=>intval(1)
	  ),
array('label'=>'Blog Posts',
	  'href'=>'/admin/resources/php/requests/forms/blog-posts.php',
	  'order'=>intval(4),	
	  'is_parent'=>intval(1),
	  'parent'=>'',
	  'id'=>'blogs-click',
	  'active'=>intval(1)
	  ),
array('label'=>'Groups &amp; Access',
	  'href'=>'/admin/resources/php/requests/forms/add-group.php',
	  'order'=>intval(4),	
	  'is_parent'=>intval(1),
	  'parent'=>'',
	  'id'=>'blogs-click',
	  'active'=>intval(1)
	  )
);
$data=new database();
$data->db(DATABASE)->collection('admin_navigation');

//echo('<pre>');
foreach($n as $k=>$v) {
$data->criteria($v)->insert();
}
$data->close();
$data=new database();
$data->db(DATABASE)->collection('admin_navigation');
$data->criteria(array('label'=>1))->uniqueIndex();
$data->close();
$data=new database();
$data->db(DATABASE)->collection('admin_navigation');
$data->criteria(array('href'=>1))->uniqueIndex();
$data->close();

function getModulesNav() {
$data=new database();
$data->db(DATABASE)->collection('admin_navigation');
$data->criteria(array('label'=>'Modules'));
$row=$data->findOne();
$data->close();
return $row;	
}
$data=new database();
$data->db(DATABASE)->collection('admin_navigation');
$m=getModulesNav();
$modules=$admin->getActiveModules();
foreach($modules['results'] as $row) {
	$item=array('label'=>$row['name'].' (module)',
	  'href'=>'/modules/'.$row['path'].'/admin/index.php',
	  'order'=>intval(1),	
	  'is_parent'=>intval(0),
	  'parent'=>(string)$m['_id'],
	  'id'=>'sub-'.$row['path'].'-click',
	  'active'=>intval(1)
	  );
$data->criteria($item)->insert();
}



$data=new database();
$data->db(DATABASE)->collection('captcha');
$data->criteria(array('word'=>1,'rand'=>1))->index();
$data->close();
//file_put_contents($_SERVER['DOCUMENT_ROOT'].'/admin/installed.txt',"Installed on ".date("d/m/y G:i:s",time())." GMT");
//rename();
rename($_SERVER['DOCUMENT_ROOT'].'/admin/installer/install.php',$_SERVER['DOCUMENT_ROOT'].'/admin/installer/installed_'.md5(microtime(true)).'.php');
?>

<script>
alert('Site has been installed');
document.location.href='/login';
</script>
